package com.lq.rocketmq.filter.tag;

import com.lq.rocketmq.config.MyConstant;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;

import java.util.List;

/**
 * @program: myrocketmq
 * @description: 消息消费者, (消息接收者)
 * @author: LQ
 * @create: 2021-05-08 10:07
 */

public class TagFilterConsumer {
    public static void main(String[] args) throws MQClientException {
        //1.创建消费者consumer，并制定消费者组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(MyConstant.MSG_GROUP);
        //2.指定nameserver地址
        consumer.setNamesrvAddr(MyConstant.NAMESRV_ADDR);
        //3.订阅Topic和Tag
        // 订阅指定 Topic 下的所有消息,多个Tag使用||连接,所有的使用*
        consumer.subscribe(MyConstant.TOPIC_TAG_FILTER, MyConstant.TAG1 + "||" +MyConstant.TAG2);
        //4.设置回调函数，处理消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            /**
             * 接收消息内容
             * @param msgs
             * @param context
             * @return
             */
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                System.out.println(msgs);
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //5.启动消费者consumer
        consumer.start();
        System.out.println("----->消费者启动");
    }

}
